package com.xiaoqi.musicplayer;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

public class MediaPlayerService extends Service {

    // 设置多媒体对象
    private MediaPlayer mediaPlayer;
    // 时钟
    private Timer timer;
    private  MusicControl musicControl;

    public MediaPlayerService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
//        // 绑定服务的时候，把音乐控制类实例化
//        return new MusicControl();
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // 实例化多媒体对象
        mediaPlayer = new MediaPlayer();
        // 启动服务的时候，把音乐控制类实例化
        musicControl = new MusicControl();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getStringExtra("action");
        switch (action) {
            case "startMusic":
                musicControl.play();
                break;
            case "pauseMusic":
                musicControl.pause();
                break;
            case "continueMusic":
              musicControl.resume();
                break;
            case "exitMusic":
                break;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        musicControl.stop();
    }

    class MusicControl extends Binder {
        // 播放音乐
        public void play() {
            mediaPlayer.reset();
            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.amp3);
            mediaPlayer.start();
            addTimer();
        }

        // 暂停
        public void pause() {
            mediaPlayer.pause();
        }

        // 继续
        public void resume() {
            // 播放 不会重置
                mediaPlayer.start();
        }

        // 停止
        public void stop() {
            mediaPlayer.stop();
            // 释放资源
            mediaPlayer.release();
            try {
                timer.cancel();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 打带
        public void seekTo(int ms) {
            mediaPlayer.seekTo(ms);
        }
    }

    // 增加计时器
    public void  addTimer(){
        if (timer == null){
            timer = new Timer();
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    int duration = mediaPlayer.getDuration();// 获取歌曲总时长
                    int currentPosition = mediaPlayer.getCurrentPosition();// 获取当前播放进度
                    Log.i("Mediaplayer","duration  "+duration);
                    Log.i("Mediaplayer","currentPosition   "+currentPosition);
                    System.out.println("发送消息成功");
                    Message message = MainActivity.handler.obtainMessage();// 创建消息对象
                    // 讲音乐总时长和播放进度封装到消息对象中
                    Bundle bundle = new Bundle();
                    bundle.putInt("duration",duration);
                    bundle.putInt("currentPosition",currentPosition);
                    message.setData(bundle);
                    // 将消息放松到主线程的消息队列
                    MainActivity.handler.sendMessage(message);
                    Log.i("Mediaplayer","发送消息成功   ");
                }
            };
            // 开始计时任务后5毫秒，第一次执行task任务，以后每500毫秒执行一次
            timer.schedule(timerTask,5,500);
        }
    }
}
